package demo.gojapn.translationdemo;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

import demo.gojapn.translationdemo.common.view.PinnedSectionListView;
import demo.gojapn.translationdemo.entity.BaseEntity;

/**
 * Created by x.chen on 2015/10/12 0012.
 */
public class PinnerListViewAdapter  extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {


    private static final int[] COLORS = new int[] {
           android. R.color.holo_blue_bright, android. R.color.holo_green_light,
            android.  R.color.holo_orange_light,  android. R.color.holo_purple };

    List<BaseEntity> items;
    private Context context;

    public PinnerListViewAdapter(Context context,List<BaseEntity> vars) {
       this.items = vars;
        this.context = context;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == BaseEntity.Type.TransSep.value();
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override public int getItemViewType(int position) {
        return getItem(position).getType().value();
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public BaseEntity getItem(int position) {
        return items.get(position) ;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView  = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, null);
        }
        TextView view = (TextView) convertView;
        view.setTextColor(Color.BLACK);
        view.setTag("" + position);
        BaseEntity item = getItem(position);
        int type = getItemViewType(position);
        switch (type){
            case 0: //sep
                view.setTextAppearance(context,android.R.style.TextAppearance_Large);
                view.setBackgroundColor(parent.getResources().getColor(COLORS[new Random(System.currentTimeMillis()).nextInt(100) % COLORS.length]));
              break;
            case 1:
                view.setTextAppearance(context,android.R.style.TextAppearance_DeviceDefault_Medium);
                break;
            default:
                break;

        }
        view.setText(items.get(position).getContent_ch());
        return view;
    }
}
